package io.github.zeleven.daysmatter;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.Date;
import java.util.List;

import static java.lang.Math.abs;

public class MainActivity extends AppCompatActivity {

    private EventDao eventDao;
    private QueryBuilder<Event> eventQuery;
    private List<Event> eventList;
    private EventAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DaoSession session = ((App) getApplication()).getDaoSession();
        eventDao = session.getEventDao();
        QueryBuilder<Event> queryBuilder = eventDao.queryBuilder();
        eventQuery = queryBuilder.orderDesc(EventDao.Properties.CreateDate);
        eventList = eventQuery.list();
        adapter = new EventAdapter(eventList);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

        setHeadContent();

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fa_button);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, EventAddActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

        QueryBuilder<Event> queryBuilder = eventDao.queryBuilder();
        eventQuery = queryBuilder.orderDesc(EventDao.Properties.CreateDate);
        eventList.clear();
        eventList.addAll(eventQuery.list());
        setHeadContent();

        adapter.notifyDataSetChanged();
    }

    private void setHeadContent() {
        TextView headEventContent = (TextView) findViewById(R.id.head_event_content);
        TextView headEventDate = (TextView) findViewById(R.id.head_event_date);
        TextView headDaysCount = (TextView) findViewById(R.id.head_days_count);
        TextView untilText = (TextView) findViewById(R.id.until_text);
        TextView distanceText = (TextView) findViewById(R.id.distance_text);
        TextView targetDateText = (TextView) findViewById(R.id.target_date_text);
        TextView daysCountText = (TextView) findViewById(R.id.days_count_text);

        Event headEvent = eventList.isEmpty() ? null : eventList.get(0);
        if(headEvent != null) {
            Date eventTargetDate = headEvent.getTargetDate();
            headEventContent.setText(headEvent.getEventContent());
            headEventDate.setText(Utility.dateToString(headEvent.getTargetDate()));
            long duration = Utility.getDateInterval(eventTargetDate);
            String text = "";
            if(duration < 0) {
                text = "已经";
            } else if(duration > 0) {
                text = "还有";
            }
            untilText.setText(text);
            headDaysCount.setText(Long.toString(abs(duration)));
        } else {
            untilText.setText("");
            distanceText.setText("");
            targetDateText.setText("");
            daysCountText.setText("");
        }
    }
}
